package com.dsbj.wx.api.utils.strprocess;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MapProcess {

	/**
	 * 将键值对存入object.mapParam中,并返回mapParamStr_new。
	 * @param mapParamStr
	 * @param key
	 * @param value
	 * @return
	 * cjw 2018-10-23
	 */
	public static String storeParamIntoMap(String mapParamStr,String key,Object value) {
		Map<String, Object> mapParam=null;
		mapParam=StrUtil.isEmpty(mapParamStr)? new LinkedHashMap<>():
			JSON.parseObject(mapParamStr, new TypeReference<LinkedHashMap<String, Object>>(){}, Feature.OrderedField);

		mapParam.put(key, value);
		return StrUtil.toPrettyJsonStr(mapParam);
	}

	/**
	 * 从mapParam中获取key关联的值。
	 *
	 * @param mapParamStr
	 * @param key
	 * @return
	 * cjw 2018-10-23
	 */
	public static String getParamValueFromMap(String mapParamStr,String key) {
		Map<String, Object> mapParam=null;
		mapParam=StrUtil.isEmpty(mapParamStr)? new LinkedHashMap<>():
			JSON.parseObject(mapParamStr, new TypeReference<LinkedHashMap<String, Object>>(){}, Feature.OrderedField);

		String paramValue= StrUtil.isEmpty(mapParam.get(key))? null: mapParam.get(key).toString();
		return paramValue;
	}

	/**
	 * 将List《Object》list_obj转化为List《Map《String,Object》》list_map。
	 * @param
	 * @return
	 * @author cjw 2019-07-25
	 */
	public static List<Map<String,Object>> convertListObject_toListMap(List<Object> list_obj){
		if(list_obj==null){
			return null;
		}

		List<LinkedHashMap<String,Object>> list_mapData=null;
		String listObjStr=null;
		listObjStr= JSON.toJSONString(list_obj, SerializerFeature.WriteMapNullValue);
		list_mapData= JSON.parseObject(listObjStr,new TypeReference<List<LinkedHashMap<String,Object>>>(){}, Feature.OrderedField);
		//将List《LinkedHashMap《String,Object》》list_LinkedHashMap转化为List《Map《String,Object》》list_map
		List<Map<String,Object>> list_map=null;
		list_map=convertListLinkedHashMap_toListMap(list_mapData);
		return list_map;
	}

	/**
	 * 将List《LinkedHashMap《String,Object》》list_LinkedHashMap转化为List《Map《String,Object》》list_map。
	 * @param
	 * @return
	 * @author cjw 2019-07-25
	 */
	public static List<Map<String,Object>> convertListLinkedHashMap_toListMap(List<LinkedHashMap<String,Object>> list_LinkedHashMap){
		if(list_LinkedHashMap==null){
			return null;
		}
		//将list_LinkedHashMap中的信息存入list_map中。
		List<Map<String,Object>> list_map=new ArrayList<>();
		for(LinkedHashMap<String,Object> linkedHashMap: list_LinkedHashMap){
			list_map.add(linkedHashMap);
		}
		return list_map;
	}

	/**
	 * 将对象转化为Map。
	 * @param
	 * @return
	 * @author cjw 2019-08-07
	 */
	public static Map<String,Object> convertObject_toMap(Object object){
		if(object==null){
			return null;
		}

		Map<String,Object> mapData=null;
		String object_str=null;
		object_str= JSON.toJSONString(object, SerializerFeature.WriteMapNullValue);
		mapData= JSON.parseObject(object_str,new TypeReference<LinkedHashMap<String,Object>>(){}, Feature.OrderedField);
		return mapData;
	}

	public static Object getObjectValueFromMap(Map<String,Object> map,String key,Class clazz){

		ObjectMapper mapper = new ObjectMapper();
		LinkedHashMap objMap = (LinkedHashMap)map.get(key);
		Object obj = null;
		if(objMap.size()>0){
			obj = mapper.convertValue(objMap,clazz);
		}
		return obj;
	}


}
